package lock.lock;

import com.sun.org.apache.regexp.internal.RE;
import sun.awt.windows.ThemeReader;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Brook
 * 用trylock来避免死锁
 */
public class TryLockDeadLock implements Runnable {

    int flag = 1;

    static Lock lock1 = new ReentrantLock();
    static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        TryLockDeadLock tryLockDeadLock1 = new TryLockDeadLock();
        TryLockDeadLock tryLockDeadLock2 = new TryLockDeadLock();
        tryLockDeadLock1.flag = 0;
        tryLockDeadLock2.flag = 1;
        Thread thread1 = new Thread(tryLockDeadLock1);
        Thread thread2 = new Thread(tryLockDeadLock2);
        thread1.start();
        thread2.start();

    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (flag == 1) {
                try {
                    if (lock1.tryLock(1000, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "获取到锁lock1");
                            Thread.sleep(new Random().nextInt(1000));
                            if(lock2.tryLock(1000, TimeUnit.MILLISECONDS)){
                                try{
                                    System.out.println(Thread.currentThread().getName() + "获取到锁lock2，all get");
                                    break;
                                }finally {
                                    lock2.unlock();
                                }
                            }else {
                                System.out.println(Thread.currentThread().getName()+"获取锁2失败");
                            }
                        } finally {
                            lock1.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    }else {
                        System.out.println(Thread.currentThread().getName()+"获取锁lcok1失败");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (flag == 0) {
                try {
                    if (lock2.tryLock(800, TimeUnit.MILLISECONDS)) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "获取到锁lock2");
                            Thread.sleep(new Random().nextInt(1000));
                            if(lock1.tryLock(800, TimeUnit.MILLISECONDS)){
                                try{
                                    System.out.println(Thread.currentThread().getName() + "获取到锁lock1，all get");
                                    break;
                                }finally {
                                    lock1.unlock();
                                }
                            }else {
                                System.out.println(Thread.currentThread().getName()+"获取锁1失败");
                            }
                        } finally {
                            lock2.unlock();
                            Thread.sleep(new Random().nextInt(1000));
                        }
                    }else {
                        System.out.println(Thread.currentThread().getName()+"获取锁lock2失败");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
